package com.ddx.myopengles20;

import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.ddx.myopengles20.newpackage.AllGLRender;

import java.io.IOException;

//http://www.cnblogs.com/renhui/p/7997557.html
public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private static final String TAG = "MainActivity";
    private GLSurfaceView mGLSurfaceView;
    private Camera camera;
    private SurfaceView cameraPreview;
    private GLSurfaceView.Renderer render;
    private SensorManager sensorManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * 开启传感器服务
         * */
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        // Create a mGLSurfaceView instance and set it
        // as the ContentView for this Activity.
//        mGLSurfaceView = new MyGLSurfaceView(this);

        /**
         * 设置渲染模块 mGLSurfaceView
         * */
        mGLSurfaceView = new GLSurfaceView(this);

        //设置渲染器
        render = new AllGLRender(mGLSurfaceView,this);

        // 创建OpenGL ES 2.0上下文
        mGLSurfaceView.setEGLContextClientVersion(2);
        mGLSurfaceView.setZOrderOnTop(true);
        mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);//这一步和下面的setFormat都是为了能够使surfaceView能够显示出来
        mGLSurfaceView.setZOrderMediaOverlay(true);
        mGLSurfaceView.setRenderer(render);
        mGLSurfaceView.setRenderMode(mGLSurfaceView.RENDERMODE_WHEN_DIRTY);//mGLSurfaceView.RENDERMODE_CONTINUOUSLY:持续渲染(默认)
        //mGLSurfaceView.RENDERMODE_WHEN_DIRTY:脏渲染,命令渲染
        mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);//设置glview为透明的

        /**
         * 设置相机预览SurfaceView
         * */
        cameraPreview = new SurfaceView(this);
        cameraPreview.getHolder().addCallback(cameraPreviewcallback);
        cameraPreview.getHolder().setFormat(PixelFormat.TRANSPARENT);


        FrameLayout layout = new FrameLayout(this);
        FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.addView(cameraPreview, params1);
        layout.addView(mGLSurfaceView, params2);
        setContentView(layout, params1);
    }


    /**
     * 传感器服务注册模块
     */
    @Override
    protected void onStop() {
        sensorManager.unregisterListener(this);
        super.onStop();
    }


    @Override
    protected void onPause() {
        sensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    protected void onResume() {
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
    }

    /**
     * 根据方向传感器的参数变化，重新渲染
     */
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        int sensorType = sensorEvent.sensor.getType();
        switch (sensorType) {
            case Sensor.TYPE_ORIENTATION:
                //获取绕三轴的角度
                float degreeZ = sensorEvent.values[0];
                float degreeX = sensorEvent.values[1];
                float degreeY = sensorEvent.values[2];
                AllGLRender.xrotate = degreeX;
                AllGLRender.yrotate = degreeY;
                AllGLRender.zrotate = degreeZ;
                Log.d(TAG, "onSensorChanged:degreeX "+degreeX);
                mGLSurfaceView.requestRender();    //请求渲染，和脏渲染配合使用
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
    }



    private SurfaceHolder.Callback cameraPreviewcallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            startPreview();
            Log.i(TAG, "surfaceCreated" + Thread.currentThread().getName());
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            Log.i(TAG, "surfaceChanged");
            camera.startPreview();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            Log.i(TAG, "surfaceDestoryed");
            if (camera != null) {
                camera.stopPreview();
                camera.release();//释放相机资源
                camera = null;
            }
        }
    };

    /**
     * 相机预览功能函数
     */
    private void startPreview() {
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(cameraPreview.getHolder());
            camera.setDisplayOrientation(90);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
